package client.comm.commlib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.util.AttributeSet;


import com.youth.banner.Banner;

import client.comm.commlib.R;

public class CornerBanner extends Banner {

    float width, height;
    float topLeftCorner = 10;
    float topRightCorner = 10;
    float bottomLeftCorner = 10;
    float bottomRightCorner = 10;

    public CornerBanner(Context context) {
        this(context, null);
    }

    public CornerBanner(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CornerBanner(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.CornerImage, defStyleAttr, 0);
        topLeftCorner = a.getDimension(
                R.styleable.CornerImage_topLeftcorner, -1);
        topRightCorner = a.getDimension(
                R.styleable.CornerImage_topRightCorner, -1);
        bottomLeftCorner = a.getDimension(
                R.styleable.CornerImage_bottomLeftcorner, -1);
        bottomRightCorner = a.getDimension(
                R.styleable.CornerImage_bottomRigthcorner, -1);
        a.recycle();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = getWidth();
        height = getHeight();
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {

        if (topLeftCorner > 0
                || topRightCorner > 0
                || bottomLeftCorner > 0
                || bottomRightCorner > 0) {
            Path path = new Path();
            //四个圆角
            path.moveTo(topLeftCorner, 0);
            path.lineTo(width - topRightCorner, 0);
            path.quadTo(width, 0, width, topRightCorner);
            path.lineTo(width, height - bottomRightCorner);
            path.quadTo(width, height, width - bottomRightCorner, height);
            path.lineTo(bottomLeftCorner, height);
            path.quadTo(0, height, 0, height - bottomLeftCorner);
            path.lineTo(0, topLeftCorner);
            path.quadTo(0, 0, topLeftCorner, 0);

            canvas.clipPath(path);
        }
        super.dispatchDraw(canvas);
    }

}
